﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using com.dz.entity;
using com.dz.common;
using System.IO;
namespace com.dz.components.background
{
    
    public class UUTxtCatchManager
    {
        DataManager m_data_mgr = new DataManager();
        FileManager m_file_mgr = new FileManager();
        Encoding m_site_encoding = Encoding.GetEncoding("gb2312");

        string m_book_list_url = "http://www.uutxt.com/Book/ShowBooklist.aspx?page={0}";
        string m_book_list_reg="<ul[^<>]*?>.*?<li[^<>]*?class=[\'\"]?h1[\'\"]?>.*?<a.*?>(.*?)</a>.*?</li>.*?<li[^<>]*?class=[\'\"]?h2[\'\"]?>.*?<a.*?>.*?</a>.*?<a[^<>]*?hrefs*=s*[\'\"s]?[^\"]*\\/(\\d+)\\.html[^\"]*[\'\"]?[^<>]*?>.*?>(.*?)<.*?</a>.*?<li[^<>]*?class=[\'\"]?h4[\'\"]?>(.*?)</li>";
        int m_category_index = 1;
        int m_book_id_index = 2;
        int m_book_name_index = 3;
        int m_author_name_index = 4;

        string m_book_intro_url = "http://www.uutxt.com/{0}.html";
        string m_book_intro_reg = "<div[^<>]*?class=[\'\"]?book_desc[\'\"]?>.*?<ul>.*?<li[^<>]*?class=[\'\"]?h1[\'\"]?>(.*?)</li>";
        string m_book_size_reg="<li[^<>]*?class=[\'\"]?h1[\'\"]?>全文长度.*?(\\d+)字</li>";
        string m_book_status_reg="<div.*?>写作进程.*?([\u4E00-\u9FFF]+).*?</div>";
        string m_book_icon_reg = "<div[^<>]*?class=[\'\"]?book_img[\'\"]?>.*?<a[^<>]*?hrefs*=s*['\"\\s]([^\"'].*?\\/\\d+\\/(\\d+)\\/index\\.shtml.*?).*?><img[^<>]*?src=['\"](.*?)\"[^<>]*?>.*?</a>.*?";

        //匹配分卷正则
         string m_volume_reg = " <div[^<>]*?id=[\'\"]?ClassTitle[\'\"]?>最新章节:(.*?)\\/.*?</div>";
 
         //章节正则
         string m_chapter_reg = "<li>.*?<a[^<>]*?href=[\'\"](\\d+)\\.shtml[\'\"]?.*?title=[\'\"]?更新时间:(\\d{4}-[\\d{2}|\\d{1}]+-[\\d{2}|\\d{1}]+\\s[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+).*?更新字数:(\\d{1,})[\'\"]?[^<>]*?>(.*?)</a>.*?</li>";
         //更新时间
         const int c_update_time_index = 1;
         //更新字数
         const int c_update_time_chaptetr_size_index = 2;
         //章节id
         const int c_chapter_id_index = 3;
         //章节名称
         const int c_chapter_name_index = 4;

         const string c_chapter_content_url = "http://files.uutxt.com/{0}/{1}/{2}.dec";

         /// <summary>
         /// 获取一个对比文件里面的节点信息
         /// </summary>
         /// <param name="i_compare_file_name"></param>
         /// <param name="i_tag_id_dictionary"></param>
         public string GetContent(string i_file_name)
         {

             //逐行读取文本内容
             string t_txt;
       
             try
             {
                 using (StreamReader t_sm_read = new StreamReader(i_file_name,

m_site_encoding))
                 {
                     t_txt = t_sm_read.ReadToEnd();
                    
                 }
                 return t_txt;
             }
             catch (Exception ex)
             {
                 return string.Empty;
             }
         }

        

        public void StartCatchBook()
        {
            //抓取到的页面代码
            string t_html = string.Empty;

            t_html = BookSiteManager.GetHttpWebResponse("http://files.uutxt.com/35/35339/2672970.dec","");
            string file_name="D:\\work\\c.txt";
            BookSiteManager.DownLoadFile("http://files.uutxt.com/35/35339/2672970.dec", file_name);
            t_html = GetContent(file_name);
            if (!string.IsNullOrEmpty(t_html))
            {
                t_html = System.Web.HttpUtility.UrlDecode(t_html, Encoding.UTF8);
            }

            //匹配的集合
            MatchCollection t_ma = null;

            //抓取的书信息
            BookInfo t_book = null;

            //书号 构造书介绍页的url
            long t_old_book_id = 0;

            try
            {
                for(int index=AppConfig.StartUUTXTPageIndex;index<AppConfig.EndUUTXTPageIndex;index++)
                {
                    t_html = BookSiteManager.ReadUrl(string.Format(m_book_list_url, index), m_site_encoding);

                    if (!string.IsNullOrEmpty(t_html))
                    {
                        t_ma = Regex.Matches(t_html, m_book_list_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                        if (t_ma != null && t_ma.Count > 0)
                        {
                            foreach (Match t_mc in t_ma)
                            {
                                t_book = new BookInfo();
                                t_book.BookSubCagegoryName = t_mc.Groups[m_category_index].Value.ToString();
                                if (!long.TryParse(t_mc.Groups[m_book_id_index].Value.ToString(), out t_old_book_id))
                                    t_old_book_id = 0;
                                t_book.OldBookId = t_old_book_id;
                                t_book.BookName = t_mc.Groups[m_book_name_index].Value.ToString();
                                t_book.AuthorName = t_mc.Groups[m_author_name_index].Value.ToString();
                                CatchBookIntro(t_book);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("抓取书籍信息失败" + ex.ToString());
            }
        }
        /// <summary>
        /// 抓取书籍介绍页面
        /// </summary>
        /// <param name="i_book"></param>
        public void CatchBookIntro(BookInfo i_book)
        {
            i_book.SiteId = (int)Consts.Site.uutxt;
            i_book.CreateTime = DateTime.Now;
            string t_html = string.Empty;
       
            string t_soso_url = string.Format(m_book_intro_url, i_book.OldBookId);
            string t_icon_url = string.Empty;
            long t_site_book_id=0;
            try
            {
                t_html = BookSiteManager.ReadUrl(t_soso_url, m_site_encoding);
                if (!string.IsNullOrEmpty(t_html))
                {
                    Match t_mc = Regex.Match(t_html, m_book_size_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                        long t_word_count = 0;
                        if (!long.TryParse(t_mc.Groups[1].Value.ToString(), out t_word_count))
                            t_word_count = 0;
                        i_book.WordsCount = t_word_count;
                    }
                    t_mc = Regex.Match(t_html,m_book_status_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                        i_book.BookStatus = t_mc.Groups[1].Value.ToString();
                    }
                    t_mc = Regex.Match(t_html, m_book_intro_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                        i_book.Description = t_mc.Groups[1].Value.ToString();
                    }
                    t_mc = Regex.Match(t_html, m_book_icon_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 2)
                    {
                        i_book.Comfrom = t_mc.Groups[1].Value.ToString();
                        if(!long.TryParse(t_mc.Groups[2].Value.ToString(),out t_site_book_id))
                            t_site_book_id=0;
                        
                        t_icon_url = t_mc.Groups[3].Value.ToString();
                        i_book.OldBookId=t_site_book_id;
                        if (t_icon_url.ToLower().IndexOf("noimg.gif") < 0)
                        {
                            i_book.IsExistsImage = true;
                        }
                      
                    }
                }
                i_book.AuthorId = 1;//升序

               long t_book_id=  m_data_mgr.AddCatchBookInterface(i_book);


               if (t_book_id > 0 && i_book.IsExistsImage)
               {
                   BookSiteManager.DownLoadImage(t_icon_url,  t_book_id);
               }
               if (t_book_id > 0)
               {
                   StartCatchChapter(i_book.Comfrom, t_book_id, i_book.OldBookId, Consts.OrderType.ASC);
               }
            }
            catch (Exception ex)
            {
                LogHelper.Error("抓取小说介绍页失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 开始抓取章节
        /// </summary>
        /// <param name="i_chapter_url"></param>
        /// <param name="i_book_id"></param>
        /// <param name="i_site_book_id"></param>
        private void StartCatchChapter(string i_chapter_url, long i_book_id, long i_site_book_id,Consts.OrderType i_order_type)
        {
            string t_soso_url = i_chapter_url;
            string t_html = string.Empty;
            MatchCollection t_ma = null;
            string t_volume_name = string.Empty;
            string t_sub_html = string.Empty;
            try
            {
                t_html = BookSiteManager.ReadUrl(t_soso_url, m_site_encoding);
                t_ma = Regex.Matches(t_html, m_volume_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_ma != null && t_ma.Count > 0)
                {
                    for (int t_index = 0; t_index < t_ma.Count - 1; t_index++)
                    {
                        t_volume_name = t_ma[t_index].Groups[1].Value.ToString();
                        t_sub_html = t_html.Substring(t_ma[t_index].Groups[1].Index, t_ma[t_index + 1].Groups[1].Index - t_ma[t_index].Groups[1].Index);

                        ParseContent(i_site_book_id, i_book_id, t_volume_name, t_sub_html, i_order_type, i_chapter_url);
                    }

                    t_volume_name = t_ma[t_ma.Count - 1].Groups[1].Value.ToString();
                    t_sub_html = t_html.Substring(t_ma[t_ma.Count - 1].Index);

                    ParseContent(i_site_book_id, i_book_id, t_volume_name, t_sub_html, i_order_type, i_chapter_url);

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取章节列表失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 解析章节
        /// </summary>
        /// <param name="i_site_book_id"></param>
        /// <param name="i_book_id"></param>
        /// <param name="i_volume_name"></param>
        /// <param name="t_html"></param>
        private void ParseContent(long i_site_book_id, long i_book_id, string i_volume_name, string i_html, Consts.OrderType i_order_type, string i_book_chapter_url)
        {
            if (string.IsNullOrEmpty(i_html))
                return;

            if (string.IsNullOrEmpty(i_volume_name))
            {
                i_volume_name = "残卷";
            }
            MatchCollection t_ma = Regex.Matches(i_html, m_chapter_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            DateTime t_update_time = DateTime.Now;
            int t_update_chapter_size = 0;
            string t_site_chapter_id = string.Empty;
            string t_chapter_name = string.Empty;
            bool t_is_vip = false;
            long t_chapter_id = 0;
            if (!string.IsNullOrEmpty(i_volume_name) && i_volume_name.ToLower().IndexOf("vip") > -1)
            {
                t_is_vip = true;
            }
            if (t_ma != null)
            {

                foreach (Match t_mc in t_ma)
                {
                    if (t_mc.Groups.Count >= c_chapter_name_index)
                    {
                        if (!DateTime.TryParse(t_mc.Groups[c_update_time_index].Value.ToString(), out t_update_time))
                            t_update_time = DateTime.Now;
                        if (!int.TryParse(t_mc.Groups[c_update_time_chaptetr_size_index].Value.ToString(), out t_update_chapter_size))
                            t_update_chapter_size = 0;
                        t_site_chapter_id = t_mc.Groups[c_chapter_id_index].Value.ToString();
                        t_chapter_name = t_mc.Groups[c_chapter_name_index].Value.ToString();

                        t_chapter_id = m_data_mgr.InsertBranckBookChapter(i_volume_name, t_chapter_name, t_update_time, t_is_vip, t_update_chapter_size, i_book_id, (int)Consts.Site.shouda8, i_site_book_id, i_book_chapter_url, (int)i_order_type);

                        //章节不存在，取章节内容
                        if (t_chapter_id > 0 && m_file_mgr.CheckChaperExist(i_book_id, t_chapter_id) == false)
                        {
                            CatchChapterContent(i_book_id, t_chapter_id, i_site_book_id, t_site_chapter_id, i_book_chapter_url);
                        }

                    }
                }
            }
        }

        /// <summary>
        /// 抓取章节内容
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        /// <param name="i_site_book_id"></param>
        /// <param name="i_site_chapter_id"></param>
        private void CatchChapterContent(long i_book_id, long i_chapter_id, long i_site_book_id, string i_site_chapter_id,string i_chapter_list_url)
        {
            string reg_category_id="\\/(\\d+)\\/";
            MatchCollection t_ma = Regex.Matches(i_chapter_list_url, reg_category_id, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            string t_category_id = "";
            if (t_ma != null && t_ma.Count > 0)
            {
                t_category_id = t_ma[0].Groups[1].Value.ToString();
            }
            else
            {
                return;
            }
            string t_content_url = string.Format(c_chapter_content_url, t_category_id, i_site_book_id, i_site_chapter_id);

            string t_html = BookSiteManager.ReadUrl(t_content_url, m_site_encoding);
            if (!string.IsNullOrEmpty(t_html))
            {
                t_html = System.Web.HttpUtility.UrlDecode(t_html);
            }
           
        }

        private string ReplaceContent(string i_text)
        {
            if (string.IsNullOrEmpty(i_text))
                return string.Empty;
            i_text = i_text.Replace("<a href=http://www.qidian.com>起点中文网 www.qidian.com 欢迎广大书友光临阅读，最新、最快、最火的连载作品尽在起点原创！</a>", "");
            i_text = i_text.Replace("ahref=http://www.&gt;起点中文网www.欢迎广大书友光临阅读，最新、最快、最火的连载作品尽在起点原创！", "");
            i_text = Regex.Replace(i_text, "<a.*?>.*?</a>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<script.*?>.*?</script>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<font.*?>.*?</font>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<div.*?>.*?</div>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return i_text;
        }
    }
}
